<?php

/**
 * category actions.
 *
 * @package    dolpheuscms
 * @subpackage category
 * @author     Harrie Essing
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class categoryActions extends sfActions
{

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request)
    {
        $treeObject = Doctrine_Core::getTable('Category')->getTree();
        $tree = $treeObject->fetchTree();

        // Here we store the level of the last item we printed out
        $lastLevel = 1;

        // Outter list item
        //$html = "<ul>";
        $html = "";

        foreach ($tree as $node) {
            // node level = 1 so we don't see the root
            if ($node['level'] == $lastLevel) {
                $html .= '</li>';
            }
            //If we are printing a next-level item, starting a new <ul>
            if ($node ['level'] > $lastLevel) {
                $html .= '<ul>';
            }
            //If we are going to return back by several levels, closing appropriate tags
            if ($node ['level'] < $lastLevel) {
                $html .= str_repeat("</li></ul>", $lastLevel - $node ['level']) . '</li>';
            }
            //Printing item without closing tag
            if ($node['level'] > 0) {
                $html .= $this->getLi($node);
            }
            //Refreshing last level of the item
            $lastLevel = $node['level'];
        }
        $this->renderText($html);
        return sfView::NONE;
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeTest(sfWebRequest $request)
    {
        //sfLoader::loadHelpers(array('Url', 'Tag'));
        $treeObject = Doctrine::getTable('Category')->getTree();
        $rootColumnName = $treeObject->getAttribute('rootColumnName');

        //Here we store the level of the last item we printed out
        $lastLevel = 0;

        //Outer list item
        $html = "<ul>";

        //Iterating all tree roots
        foreach ($treeObject->fetchRoots() as $root) {
            $options = array('root_id' => $root->$rootColumnName);

            //Iterating tree from a current root
            foreach ($treeObject->fetchTree($options) as $node) {

                //If we are on the item of the same level, closing <li> tag before printing item
                if (($node ['level'] == $lastLevel) and ($lastLevel > 0)) {
                    $html .= '</li>';
                }

                //If we are printing a next-level item, starting a new <ul>
                if ($node ['level'] > $lastLevel) {
                    $html .= '<ul>';
                }

                //If we are going to return back by several levels, closing appropriate tags
                if ($node ['level'] < $lastLevel) {
                    $html .= str_repeat("</li></ul>", $lastLevel - $node ['level']) . '</li>';
                }

                //Priting item without closing tag
                $html .= $this->getLi($node);

                //Refreshing last level of the item
                $lastLevel = $node ['level'];
            }

            //$this->renderText ( $html );
        }
        $html .= "</ul>";
        $this->renderText($html);
        return sfView::NONE;
    }

    /**
     * Function builds <li> HTML representation for a given node
     * Closing tag (/li) is not included
     * @param array $node
     */
    private function getLi($node)
    {
        return '<li id="CatalogCategory"><a href="' . $node['name'] . '">' . $node['name'] . '</a>';
    }

}
